<template>
  <ContentWrap ref="form">
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="区域" prop="parentDeptId">
        <el-tree-select
          v-model="queryParams.parentDeptId"
          :data="deptList"
          :props="defaultProps"
          check-strictly
          node-key="id"
          placeholder="请选择区域"
          filterable
          @change="getList"
          clearable
        />
      </el-form-item>
      <el-form-item label="学校名称" prop="schoolName">
        <el-input
          v-model="queryParams.schoolName"
          placeholder="请输入学校名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="学校编码" prop="schoolCode">
        <el-input
          v-model="queryParams.schoolCode"
          placeholder="请输入学校编码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="学校类型" prop="schoolType">
        <el-select
          v-model="queryParams.schoolType"
          placeholder="请选择状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.CMIS_SCHOOL_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="办学性质" prop="schoolNature">
        <el-select
          v-model="queryParams.schoolNature"
          placeholder="请选择状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.CMIS_SCHOOL_NATURE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="学校类型(小学/初中/九年一贯制)" prop="schoolType">
        <el-select
          v-model="queryParams.schoolType"
          placeholder="请选择学校类型(小学/初中/九年一贯制)"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable class="!w-240px">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="省" prop="province">
        <el-input
          v-model.tirm="queryParams.province"
          placeholder="请输入省"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="市" prop="city">
        <el-input
          v-model.tirm="queryParams.city"
          placeholder="请输入市"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="区域" prop="region">
        <el-input
          v-model.tirm="queryParams.region"
          placeholder="请输入区域"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="属性" prop="attribute">
        <el-input
          v-model.tirm="queryParams.attribute"
          placeholder="请输入属性"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="学校地址" prop="address">
        <el-input
          v-model.tirm="queryParams.address"
          placeholder="请输入学校地址"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="学校联系方式" prop="contact">
        <el-input
          v-model.tirm="queryParams.contact"
          placeholder="请输入学校联系方式"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="经度" prop="longitude">
        <el-input
          v-model.tirm="queryParams.longitude"
          placeholder="请输入经度"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="纬度" prop="latitude">
        <el-input
          v-model.tirm="queryParams.latitude"
          placeholder="请输入纬度"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" />
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" />
          重置
        </el-button>
        <el-button type="primary" @click="openForm('create')" v-hasPermi="['cmis:school:create']">
          <Icon icon="ep:plus" class="mr-5px" />
          新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['cmis:school:export']"
        >
          <Icon icon="ep:download" class="mr-5px" />
          导出
        </el-button>
        <el-button type="danger" @click="handleDeleteMulti" v-hasPermi="['cmis:school:delete']">
          <Icon icon="ep:delete" class="mr-5px" /> 批量删除
        </el-button>
        <el-button
          type="warn"
          plain
          @click="handleSync"
          :loading="syncLoading"
          v-hasPermi="['cmis:csbusiness:sync-base-data']"
        >
          <Icon icon="ep:plus" class="mr-5px" />
          同步基础信息
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap :setPadding="false">
    <el-table
      v-loading="loading"
      :data="list"
      @selection-change="selectionChange"
      stripe
      :max-height="tableHeight"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column label="区域" prop="parentDeptName" show-overflow-tooltip />
      <el-table-column label="学校名称" prop="schoolName" show-overflow-tooltip />
      <el-table-column label="学校编码" prop="schoolCode" show-overflow-tooltip />
      <el-table-column label="学校类型" prop="schoolType">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.CMIS_SCHOOL_TYPE" :value="scope.row.schoolType" />
        </template>
      </el-table-column>
      <el-table-column label="办学性质" prop="schoolNature">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.CMIS_SCHOOL_NATURE" :value="scope.row.schoolNature" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="省" prop="province" />
      <el-table-column label="市" prop="city" />
      <el-table-column label="区域" prop="region" />
      <el-table-column label="属性" prop="attribute" />
      <el-table-column label="学校地址" prop="address" />
      <el-table-column label="学校联系方式" prop="contact" />
      <el-table-column label="学校简介" prop="schoolProfile" />
      <el-table-column label="经度" prop="longitude" />
      <el-table-column label="纬度" prop="latitude" /> -->
      <el-table-column
        label="创建时间"
        prop="createTime"
        :formatter="dateFormatter"
        width="180"
        show-overflow-tooltip
      />
      <el-table-column label="操作" fixed="right" width="100">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['cmis:school:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['cmis:school:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <SchoolForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts" name="School">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import * as SchoolApi from '@/api/cmis/school'
import * as CsBusinessApi from '@/api/cmis/csbusiness'
import SchoolForm from './SchoolForm.vue'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import { defaultProps, handleTree } from '@/utils/tree'
import * as DeptApi from '@/api/system/dept'
const form = ref(null)
const { width, height } = useElementSize(form)
const tableHeight = useWindowSize(78 + 76, height)

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  createTime: [],
  schoolName: null,
  schoolCode: null,
  schoolType: null,
  schoolNature: null,
  status: null,
  parentDeptId: null,
  sourceType: null,
  deptId: null,
  province: null,
  city: null,
  region: null,
  attribute: null,
  address: null,
  contact: null,
  schoolProfile: null,
  longitude: null,
  latitude: null
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
const syncLoading = ref(false) // 同步基础数据的加载中
const deptList = ref<Tree[]>([]) // 树形结构

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await SchoolApi.getSchoolPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await SchoolApi.deleteSchool(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await SchoolApi.exportSchool(queryParams)
    download.excel(data, '学校.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}
/** 同步基础数据按钮操作 */
const handleSync = async () => {
  try {
    // 导出的二次确认
    await message.syncConfirm()
    // 发起导出
    syncLoading.value = true
    await CsBusinessApi.syncBaseData()
    await getList()
  } finally {
    syncLoading.value = false
  }
}

const listOfIds = ref([]) //选中的设备

// 选中设备
const selectionChange = (val: any) => {
  listOfIds.value = val.map((item: any) => item.id)
}

// 批量删除操作
const handleDeleteMulti = async () => {
  if (listOfIds.value.length > 0) {
    try {
      // 批量删除的二次确认
      await message.delConfirm()
      // 发起批量删除
      await SchoolApi.batchDeleteSchool(listOfIds.value)
      message.success(t('common.delSuccess'))
      // 刷新列表
      await getList()
    } catch {}
  } else {
    message.warning(t('请选择要删除的学校!'))
  }
}

/** 初始化 **/
onMounted(async () => {
  getList()
  // 加载部门树
  deptList.value = handleTree(await DeptApi.getSimpleAreaDeptList())
})
</script>
